package geo.com.geosdkdemo.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class BlueToothBroadcastReceiver extends BroadcastReceiver {
    private Context mContext;
    private BluetoothDevice device;
    private IBlueToothDeviceFoundListener iBlueToothDeviceFoundListener;
    private IBlueToothOperateListener iBlueToothOperateListener;
    private IBondStateListener iBondStateListener;
    private static BlueToothBroadcastReceiver singleton;
    //防止外部new
    public BlueToothBroadcastReceiver() {

    }

    //单例模式
    public static BlueToothBroadcastReceiver getSingleton() {
        if (singleton == null) {  //第一次检查，避免不必要的同步
            synchronized (BlueToothBroadcastReceiver.class) {  //同步
                if (singleton == null) {   //第二次检查，为null时才创建实例
                    singleton = new BlueToothBroadcastReceiver();
                }
            }
        }
        return singleton;
    }

    public void setBlueToothOperateListener(IBlueToothOperateListener listener){
        this.iBlueToothOperateListener =listener;
    }

    public void setBlueToothDeviceFoundListener(IBlueToothDeviceFoundListener listener){
        this.iBlueToothDeviceFoundListener =listener;
    }

    public void setBlueToothBondListener(IBondStateListener listener){
        this.iBondStateListener =listener;
    }

//    public BlueToothBroadcastReceiver(Context context){
//        this.mContext=context;
//    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {//找到设备 把找到的设备添加进ListView
            device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(iBlueToothDeviceFoundListener!=null){
                iBlueToothDeviceFoundListener.onBlueToothDeviceFound(device,intent);
            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            if(iBlueToothDeviceFoundListener!=null){
                iBlueToothDeviceFoundListener.onBlueToothSearching();
            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            if(iBlueToothDeviceFoundListener!=null){
                iBlueToothDeviceFoundListener.onBlueToothSearched();
            }
        }else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            switch (state) {
                //成功开启蓝牙
                case BluetoothAdapter.STATE_ON:
                    if(iBlueToothOperateListener!=null){
                        iBlueToothOperateListener.openedBlueTooth();
                    }
                    break;
                //成功关闭蓝牙
                case BluetoothAdapter.STATE_OFF:
                    //通知连接页面
                    if(iBlueToothOperateListener!=null){
                        iBlueToothOperateListener.closedBlueTooth();
                    }
                    //通知MainActivity
                    break;
                default:
                    break;
            }
        }else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { // 配对状态改变
            int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
            switch (state) {
                case BluetoothDevice.BOND_BONDED:
                    if(iBondStateListener!=null){
                        iBondStateListener.bonded();
                    }
                    break;
                case BluetoothDevice.BOND_BONDING:
                    if(iBondStateListener!=null){
                        iBondStateListener.bonding();
                    }
                    break;
                case BluetoothDevice.BOND_NONE:
                    if(iBondStateListener!=null){
                        iBondStateListener.bondNone();
                    }
            }
        }
    }

}
